import { defineConfig, loadEnv } from 'vite'
import viteBaseConfig from './config/vite.base.config.js'
import viteProdConfig from './config/vite.prod.config.js'
import viteDevConfig from './config/vite.dev.config.js'
import viteTestConfig from './config/vite.test.config.js'

const envResolver = {
  build: (env) => {
    // 生产环境
    if (env.ENV_MODE == 'test') {
      return Object.assign({}, viteBaseConfig, viteTestConfig)
    }
    return Object.assign({}, viteBaseConfig, viteProdConfig)
  },
  serve: () => {
    // 开发环境
    return Object.assign({}, viteBaseConfig, viteDevConfig)
  }
}

export default defineConfig(({ command, mode }) => {
  const env = loadEnv(mode, process.cwd(), '') // 注入自定义环境变量
  const now = new Date()
  console.log(
    `------------${env.APP_MODE}-------------\n   当前时间:${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()} ${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`
  )
  return envResolver[command](env)
})
